import com.rectcircle.scalatrateset._
import org.scalatra._
import javax.servlet.ServletContext

import _root_.akka.actor.{ActorSystem, Props}
import com.rectcircle.scalatrateset.actor.MyActor
import com.rectcircle.scalatrateset.config.DbConfig
import com.rectcircle.scalatrateset.controller._
import org.scalatra.metrics.MetricsBootstrap
import org.scalatra.metrics.MetricsSupportExtensions._

class ScalatraBootstrap extends LifeCycle with MetricsBootstrap with DbConfig  {
  
  val system = ActorSystem()
  val myActor = system.actorOf(Props[MyActor])
  implicit val swagger = new FlowersSwagger
  
  
  override def init(context: ServletContext) {
    initDbConnection
    context.mount(new MainServlet, "/*") //测试路由
    context.mount(new ActionController,"/action") //测试action
    context.mount(new ReverseRouteController,"/redirect") //测试重定向
    context.mount(new GzipController,"/gzip") //测试开启gzip压缩传输
    context.mount(new AdminController,"/admin") //测试Authentication登录验证框架
    context.mount(new LoginController,"/login") //测试Authentication登录验证框架
    context.mount(new FlashController,"/flash") //测试重定向参数传递
    context.mount(new AkkaController(system, myActor), "/akka") //测试异步akka支持
    context.mount(new ChatController, "/chat") //测试websocket框架Atmosphere
    context.mount(new ViewController, "/view") //测试视图渲染模块
    context.mount(new UploadController, "/upload") //测试文件上传
    context.mount(new JsonController, "/json") //测试json自动转换
    context.mount(new TodoController, "/todos") //测试command（失败）
    context.mount(new DbController, "/db") //测试继承orm框架之一squeryl
    
    context.mount(new FlowersController, "/flowers", "flowers") //测试restful api 文档生成器Swagger
    context.mount (new ResourcesApp, "/api-docs") ////测试restful api 文档生成器Swagger
  
    //测试程序运行监控框架Metrics
    context.mountMetricsAdminServlet("/metrics-admin")
    context.mountHealthCheckServlet("/health")
    context.mountMetricsServlet("/metrics")
    context.mountThreadDumpServlet("/thread-dump")
    context.installInstrumentedFilter("/test/*")
  
    context.mount (new TestController, "/test")
  
    //测试log使用
    context.mount (new LogController, "/log")
    
  
  }
  
  override def destroy(context:ServletContext) {
    closeDbConnection()
    system.terminate()
  }
  
}
